package com.gking.centerSystem.controller;

import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.User;
import com.gking.centerSystem.entity.dao.PagingQueryUserDao;
import com.gking.centerSystem.entity.dto.UserRoleListDto;
import com.gking.centerSystem.service.SystemInfo.SystemInfoService;
import com.gking.centerSystem.service.UserRoles.UserRolesService;
import com.gking.centerSystem.utils.GetAuthInfoUtil;
import com.gking.centerSystem.utils.LimitAccessUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/userRoles")
public class UserRolesController {

    @Resource
    private UserRolesService userRolesService;

    @Resource
    private SystemInfoService systemInfoService;

    /**
     * 获取用户角色详情
     *
     * @param userId 用户id
     * @return s
     */
    @RequiresPermissions("CS:manager:get:userRoles")
    @GetMapping("/getUserRoles/{userId}")
    public Result<UserRoleListDto> getUserRolesDetail(@PathVariable String userId) {
        return userRolesService.getUserRolesDetail(userId);
    }

    /**
     * 获取用户角色详情 (设置访问ip白名单)
     *
     * @param userId 用户id
     * @return s
     */
    @RequiresPermissions("CS:manager:get:userRoles")
    @GetMapping("/limit/getUserRoles/{userId}")
    public Result<UserRoleListDto> getUserRolesDetail2(@PathVariable String userId, HttpServletRequest request) {
        Result<List<String>> systemIpList = systemInfoService.getSystemIpList();
        boolean b = LimitAccessUtil.remoteSubAccess(GetAuthInfoUtil.getClientIp(request), systemIpList.getData());
        if (!b) return Result.error("无法访问！");

        return userRolesService.getUserRolesDetail(userId);
    }

    /**
     * 为用户设置角色
     *
     * @param userId     用户id
     * @param type       类型 UserRoleSetType（refresh, add, remove）
     * @param roleIdList 角色id 列表
     * @return s
     */
    @RequiresPermissions("CS:manager:set:userRoles")
    @PostMapping("/setUserRoles")
    public Result<String> setUserRoles(@RequestParam String userId, @RequestParam String type,
                                       @RequestBody List<String> roleIdList) {
        return userRolesService.setUserRoles(userId, type, roleIdList);
    }

    /**
     * 获取角色用户详情
     *
     * @param pagingQueryUserDao 分页查询参数
     * @return s
     */
    @RequiresPermissions("CS:manager:set:userRoles")
    @PostMapping("/getRoleUsersDetail")
    public Result<List<User>> getRoleUsersDetail(@RequestBody PagingQueryUserDao pagingQueryUserDao) {

        String roleId = pagingQueryUserDao.getRoleId();
        String account = pagingQueryUserDao.getAccount();
        String nickname = pagingQueryUserDao.getNickname();

        return userRolesService.getRoleUsersDetail(roleId, account, nickname);
    }

    /**
     * 为角色设置用户
     *
     * @param roleId     角色id
     * @param type       类型 UserRoleSetType（refresh, add, remove）
     * @param userIdList 用户id 列表
     * @return s
     */
    @RequiresPermissions("CS:manager:set:userRoles")
    @PostMapping("/setRoleUsers")
    public Result<String> setRoleUsers(@RequestParam String roleId, @RequestParam String type,
                                       @RequestBody List<String> userIdList) {
        return userRolesService.setRoleUsers(roleId, type, userIdList);
    }

}
